import requests
def GetCodeList():
    try:
        f=open('../conf/reporterAreas.json')
        f.close()
    except IOError:
        content = requests.get("https://comtrade.un.org/Data/cache/reporterAreas.json")
        with open('../conf/reporterAreas.json','wb') as fp:
            fp.write(content.content)
    try:
        f=open('../conf/classificationH4.json')
        f.close()
    except IOError:
        content = requests.get("https://comtrade.un.org/Data/cache/classificationH4.json")
        with open('../conf/classificationH4.json','wb') as fp:
            fp.write(content.content)

import json
from conf import config
def DownloadList():
    try:
        f=open('../cache/cache.json')
        f.close()
        print('CacheList existed')
    except IOError:
        with open('../conf/reporterAreas.json','r',encoding='utf-8-sig') as f:
            RawDig=json.load(fp=f)
        RDict=dict()
        DropList=list()
        DropList.append('all')
        if config.start>1964:
            DropList.append('835')
            DropList.append('836')
            if config.start>1974:
                DropList.append('866')
                DropList.append('868')
                DropList.append('230')
                if config.start>1980:
                    DropList.append('717')
                    if config.start>1981:
                        DropList.append('841')
                        if config.start>1989:
                            DropList.append('886')
                            DropList.append('720')
                            DropList.append('278')
                            DropList.append('280')
                            if config.start>1991:
                                DropList.append('810')
                                DropList.append('890')
                                if config.start>1993:
                                    DropList.append('582')
                                    if config.start>1998:
                                        DropList.append('590')
                                        DropList.append('592')
                                        if config.start>2011:
                                            DropList.append('736')
        Period=list()
        for i in range(config.start,config.end+1):
            Period.append(str(i))
        for i in RawDig.get('results'):
            if i.get('id') in DropList:
                continue
            tdata={i.get('text'):Period}
            data={i.get('id'):tdata}
            RDict.update(data)
        with open('../cache/cache.json','w') as f:
            f.write(json.dumps(RDict,indent=4))
    with open('../cache/cache.json','r') as f:
        RDict=json.load(fp=f)